﻿@using Metrics = Aspire.Dashboard.Resources.Metrics

@inject IStringLocalizer<Metrics> Loc

@if (PageViewModel.Instruments is null)
{
    return;
}

@if (IncludeLabel)
{
    <FluentInputLabel Label="@Loc[nameof(Metrics.MetricsInsturementNameGridNameColumnHeader)]" ForId="metric-selector" />
}

<FluentTreeView Id="metric-selector" Class="metrics-tree" @bind-CurrentSelected="@PageViewModel.SelectedTreeItem" @bind-CurrentSelected:after="HandleSelectedTreeItemChangedAsync">
    <ChildContent>
        @foreach (var meterGroup in PageViewModel.Instruments.GroupBy(i => i.Parent).OrderBy(g => g.Key.Name))
        {
            <FluentTreeItem @key="meterGroup.Key" Text="@meterGroup.Key.Name" Data="@meterGroup.Key" title="@meterGroup.Key.Name" InitiallyExpanded="true" InitiallySelected="@(PageViewModel.SelectedInstrument == null && meterGroup.Key.Name == PageViewModel.SelectedMeter?.Name)">
                @foreach (var instrument in meterGroup.OrderBy(i => i.Name))
                {
                    <FluentTreeItem @key="instrument" Class="nested" Text="@instrument.Name" Data="@instrument" title="@instrument.Name" InitiallySelected="@(instrument.Name == PageViewModel.SelectedInstrument?.Name && instrument.Parent.Name == PageViewModel.SelectedMeter?.Name)"/>
                }
            </FluentTreeItem>
        }
    </ChildContent>
</FluentTreeView>
